//
//  ViewController.m
//  sqliteDemo
//
//  Created by wulanzhou on 2017/3/29.
//  Copyright © 2017年 wulanzhou. All rights reserved.
//

#import "ViewController.h"
#import "SQLDBManager.h"
#import "BSQLConfig.h"
#import "BookDBService.h"

@interface ViewController ()
@property (nonatomic,strong) BookDBService *dbService;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BookDBService *)dbService{

    if (!_dbService) {
        
        _dbService = [[BookDBService alloc] init];
    }
    return _dbService;
}

//创建数据库
- (IBAction)createClick:(id)sender {
    
    BOOL success = [[SQLDBManager shareInstance] createDBWithName:@"book.sqlite" sqlTables:@[book_table_sql,catalog_table_sql,chapter_table_sql]];
    if(success){
        NSLog(@"创建db成功!");
    }else{
        NSLog(@"创建db失败!");
    }
}

//新增数据
- (IBAction)addClick:(id)sender {
    [self.dbService addBookName:@"蛮慌纪" author:@"我吃西红柿"];
}

- (IBAction)updateClick:(id)sender {
    
    Book *book = [[Book alloc] init];
    book.bId = 1;
    book.status = BookStatusCatalog;
    book.mtime = [NSDate date];
    
    [self.dbService Update:book];
}

//查询数据
- (IBAction)queryClick:(id)sender {
    
    Book *book = [self.dbService Entity:[NSNumber numberWithInteger:1]];
    if (book) {
        NSLog(@"书籍名称：%@",book.name);
    }else{
        NSLog(@"查询不存在!");
    }
    
    
}
@end
